'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 提升士兵的天赋
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var moment = require('moment');
var dataApi = require('../../../../libs/dataApi.js');
var Code = require('../../../../consts/code');
var buildService = require('../../../../services/buildService');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var soldierConf = require('../../../../config/soldierConf');
var buildConf = require('../../../../config/buildConf.js');
var buildType = require('../../../../consts/buildType');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var soldierService = require('../../../../services/soldierService.js');
var arenaService = require('../../../../services/arenaService.js');
var marqueeConf = require('../../../../config/marqueeConf.js');
var marqueeModel = require('../../../../models/marqueeModel.js');
var activityService = require('../../../../services/activityService.js');
var actType = require('../../../../consts/activityType.js');

var max_soldier_star = 5;

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var soldier_type = msg.soldier_type;
    var star_lv = msg.star_lv;
    if (!soldier_type || !star_lv || star_lv < 1 || star_lv > 5) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var talent_idx = star_lv - 1;
    var player_manager = new PlayerManager(role_id);

    // jshint maxstatements:100
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'build', 'arena', 'soldier', 'task', 'daily_task', 'achieve', 'bag', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var soldier_model = player_manager.soldier;
                var bag_model = player_manager.bag;
                var build_model = player_manager.build;

                var soldier = soldier_model.getSoldier(soldier_type);

                // 是否存在这种士兵
                if (!soldier) {
                    return next(null, {
                        code: Code.SOLDIER_NOT_EXIST
                    });
                }

                // 天赋开启判断
                var talent_type_lv = soldier.talent[talent_idx];
                var talent_lv = talent_type_lv % 100;
                if (talent_lv === 0) {
                    return next(null, {
                        code: Code.SOLDIER_TALENT_NOT_OPEN
                    });
                }

                // 取出下一级配置
                var next_talent_type_lv = talent_type_lv + 1;
                var talent_conf = soldierConf.getSoldierTalentLvConf(next_talent_type_lv);
                if (!talent_conf) {
                    return next(null, {
                        code: Code.SOLDIER_TALENT_LV_REACH_MAX
                    });
                }

                // 判断士兵学院等级
                var soldier_college_build = build_model.getBuildByType(buildType.SOLDIERCOLLEGE);
                var build_conf = buildConf.getBuildLvConf(buildType.SOLDIERCOLLEGE, soldier_college_build.getLv());
                if (talent_lv > build_conf.soldier_talent_lvl_limit) {
                    return next(null, {
                        code: Code.BUILD_SOLDIER_COLLEGE_LV_NOT_ENOUGH
                    });
                }

                // 天赋升级所需材料
                var talent_cost_conf = soldierConf.getUpgradeTalentCostConf(star_lv, talent_lv);
                if (!talent_cost_conf) {
                    return next(null, {
                        code: Code.SOLDIER_TALENT_INVALID_STAR_LV
                    });
                }
                var property_type = talent_cost_conf.property;
                var cost_num = talent_cost_conf.num;

                // 扣除升级材料
                if (!bag_model.subProperty(property_type, cost_num)) {
                    return next(null, {
                        code: Code.SOLDIER_TALENT_NOT_ENOUGH_COST
                    });
                }

                // 升级天赋
                soldier.upgradeTalent(talent_idx);

                // 更新战力
                soldierService.updateSoldierPower(player_manager);
                soldierService.refreshWallSoldierPower(player_manager);

                // 更新竞技场战力
                arenaService.uupdateArenaPower(player_manager, 1);
                arenaService.uupdateArenaPower(player_manager, 2);

                // 士兵天赋统计
                taskService.addCompleteSoldierTalent(player_manager);

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
